package br.edu.utfpr.sgbd;

import java.util.Properties;

import org.hibernate.cfg.Configuration;

import br.edu.utfpr.app.beans.Cliente;
import br.edu.utfpr.app.beans.Pedido;
import br.edu.utfpr.app.beans.Produto;
import br.edu.utfpr.app.beans.Vendedor;
public class HibernateConfiguration
{
	public Configuration getConfiguration()
	{
		System.out.println("HibernateConfiguration.getConfiguration()");
		Configuration ac = new Configuration();
		addProperties(ac);
		addMapping(ac);
		return ac;
	}
	
	private static void addMapping(Configuration ac)
	{
		ac.addAnnotatedClass(Cliente.class);
		ac.addAnnotatedClass(Vendedor.class);
		ac.addAnnotatedClass(Produto.class);
		ac.addAnnotatedClass(Pedido.class);
	}
	
	private static void addProperties(Configuration ac)
	{
		ac.addProperties(poolConnectionsProperties());
		ac.addProperties(secondLevelCacheProperties());
		ac.addProperties(sqlProperties());
		ac.addProperties(databaseConnectionProperties());
		System.out.println(ac.getProperty("hibernate.dialect"));
	}
	
	private static Properties sqlProperties()
	{
		Properties properties = new Properties();
		properties.setProperty("hibernate.show_sql", "false");
		properties.setProperty("hibernate.format_sql", "true");
		properties.setProperty("hibernate.use_sql_comments", "true");
		properties.setProperty("hibernate.query.substitutions", "yes 'Y', no 'N'");
		return properties;
	}
	
	private static Properties poolConnectionsProperties()
	{
		Properties properties = new Properties();
		properties.setProperty("hibernate.c3p0.max_size", "100");
		properties.setProperty("hibernate.c3p0.min_size", "5");
		properties.setProperty("hibernate.c3p0.timeout", "60");
		properties.setProperty("hibernate.c3p0.max_statements", "50");
		properties.setProperty("hibernate.c3p0.acquire_increment", "5");
		properties.setProperty("hibernate.c3p0.validate", "false");
		properties.setProperty("hibernate.connection.provider_class", "org.hibernate.connection.C3P0ConnectionProvider");
		return properties;
	}
	
	private static Properties secondLevelCacheProperties()
	{
		Properties properties = new Properties();
		properties.setProperty("hibernate.cache.use_second_level_cache", "true");
		properties.setProperty("hibernate.cache.use_query_cache", "true");
		properties.setProperty("hibernate.cache.provider_class", "org.hibernate.cache.EhCacheProvider");
		return properties;
	}
	
	private static Properties databaseConnectionProperties()
	{
		
		System.out.println("HibernateConfiguration.databaseConnectionProperties()");
		Properties properties = new Properties();
		
		String dialeto = SocketsToolsPropriedades.getInstance().get(Configuracoes.HIBERNATE_DIALECT);//FIXME
		properties.setProperty("hibernate.dialect", dialeto);
		
		String driver = SocketsToolsPropriedades.getInstance().get(Configuracoes.HIBERNATE_CONNECTION_DRIVER_CLASS);//FIXME
		properties.setProperty("hibernate.connection.driver_class", driver);
		
		properties.setProperty("hibernate.connection.url", getUlrConexaoBanco());
		
		String usuario = SocketsToolsPropriedades.getInstance().get(Configuracoes.USUARIO_BANCO);//FIXME
		String senha = SocketsToolsPropriedades.getInstance().get(Configuracoes.SENHA_BANCO);//FIXME
		
		properties.setProperty("hibernate.connection.username", usuario);
		properties.setProperty("hibernate.connection.password", senha);
		properties.setProperty("hibernate.current_session_context_class", "thread");
		return properties;
	}
	
	/**
	 * Monta a url de conexao com o banco de dados com base nas configurações do arquivo de propriedades
	 * 
	 * @return
	 */
	private static String getUlrConexaoBanco()
	{
		String host = SocketsToolsPropriedades.getInstance().get(Configuracoes.HOST_BANCO);//FIXME
		String porta = SocketsToolsPropriedades.getInstance().get(Configuracoes.PORTA_BANCO);//FIXME
		String nomeBanco = SocketsToolsPropriedades.getInstance().get(Configuracoes.NOME_BANCO);//FIXME
		
		StringBuilder con = new StringBuilder();
		
		con.append("jdbc:postgresql://");
		con.append(host);
		con.append(":");
		con.append(porta);
		con.append("/");
		con.append(nomeBanco);
		con.append("?charSet=UTF8");
		
		return con.toString();
	}
}
